.SUFFIX: .a .axf .map .bin .scat .via .cfg .o .objs .txt

### The Name of Project
PRJ_NAME        := RS_MILBCM0
export PRJ_NAME

### ARM core (used by option)
ARMCORE         := cortex-m0
export ARMCORE

TCHAIN  := $(GNUarm)

# Use newlib-nano. To disable it, specify USE_NANO=
USE_NANO=--specs=nano.specs

### Root Directory of the Project
ROOT_DIR        := ..

### The Name of Directory
WS_DIR_NAME      := Project
export WS_DIR_NAME

OBJ_DIR_NAME     := obj
export OBJ_DIR_NAME

LIB_DIR_NAME     := lib

BIN_DIR_NAME     := bin

### The List of Sub-Directory which have Makefile
SUBDIRS         := $(ROOT_DIR)/Base \
                   $(ROOT_DIR)/CMSIS \
                   $(ROOT_DIR)/FreeRTOS \
                   $(ROOT_DIR)/Custom \

### Library
LIBS           := $(ROOT_DIR)/MILBCM0_API/lib/MILBCM0_API.a
LIBS           += $(ROOT_DIR)/MILBCM0_API/MILBCM0_Header/lib/MILBCM0.a

### File (related to armlink)
### note : If you want to make library file, Variable "ELF_FILE" must be empty.
ELF_FILE        = $(BIN_DIR_NAME)/$(PRJ_NAME).axf
LIST_FILE       = $(BIN_DIR_NAME)/$(PRJ_NAME).map
SCAT_FILE       = gcc_arm.ld

### File (related to fromelf)
CVT_FILE	= $(BIN_DIR_NAME)/$(PRJ_NAME).bin
CVT_FILE_SDRAM	= $(BIN_DIR_NAME)/$(PRJ_NAME)_SDRAM.bin
CVT_FILE_DATA	= $(BIN_DIR_NAME)/$(PRJ_NAME)_DATA.bin
CVT_SDRAM_SEC   = --only-section=.text
CVT_SDRAM_SEC  += --only-section=.rodata
CVT_SDRAM_SEC  += --only-section=.ARM.exidx
CVT_SDRAM_SEC  += --only-section=.rodata.str1.4
CVT_SDRAM_SEC  += --only-section=.bss
CVT_SDRAM_SEC  += --only-section=.heap
CVT_SDRAM_SEC  += --only-section=.stack
###CVT_SDRAM_SEC  += --only-section=.ARM.attributes
CVT_DATA_SEC	= --only-section=.data
###CVT_DATA_SEC   += --only-section=MPKID_FS_ZI_AREA

### Linker
LD       := $(TCHAIN)gcc
LDFLAGS  := $(USE_NANO)
LDFLAGS  += -mthumb
LDFLAGS  += -mcpu=$(ARMCORE)
LDFLAGS  += -T./$(SCAT_FILE)
LDFLAGS  += -n
LDFLAGS  += -Wl,--start-group -lc -lnosys -Wl,--end-group -static 
LDFLAGS  += -Wl,-Map=$(LIST_FILE),--cref,--gc-sections -symbolic
OBJS       = $(patsubst %.o, $(ROOT_DIR)/%.o, $(shell cat $(ROOT_DIR)/Project/$(OBJ_DIR_NAME)/*.objs 2>/dev/null))

### Librarian
AR       := $(TCHAIN)ar
ARFLAGS   = -r

### Conveting Tool
CV       := $(TCHAIN)objcopy
CVFLAGS   = -Obinary


ifeq "$(ELF_FILE)" ""
DEFAULT_TARGET := lib
else
DEFAULT_TARGET := target
endif

QUIET := @

.PHONY: default all message target clean clean-obj depends distclean lib recursive-target $(SUBDIRS) \
        message-self depends-self target-self clean-self clean-obj-self distclean-self lib-self \
        config convert clean-elf tool_version

default : $(DEFAULT_TARGET)

all : clean depends target convert

lib : clean depends target lib-self

message : 
	$(QUIET)$(MAKE) recursive-target PRE_TARGET=message-self

target :
	$(QUIET)$(MAKE) recursive-target POST_TARGET=target-self IS_INCLUDE_DEPS=y

clean : 
	$(QUIET)$(MAKE) recursive-target POST_TARGET=clean-self

clean-obj : 
	$(QUIET)$(MAKE) recursive-target POST_TARGET=clean-obj-self

distclean : 
	$(QUIET)$(MAKE) recursive-target POST_TARGET=distclean-self

depends : 
	$(QUIET)$(MAKE) recursive-target POST_TARGET=depends-self

recursive-target : $(PRE_TARGET) $(SUBDIRS) $(POST_TARGET)

ifneq "$(SUBDIRS)" ""
$(SUBDIRS) :
	$(QUIET)$(MAKE) recursive-target -C $@
endif


message-self : 
ifneq "$(MESSAGE)" ""
	$(QUIET)echo $(MESSAGE)
endif


clean-elf : 
	$(QUIET)if [ $(ELF_FILE) ]; then rm -rf $(ELF_FILE); fi;

depends-self : 

target-self : $(CVT_FILE) lib-self

$(ELF_FILE) : $(OBJS) $(SCAT_FILE) $(LIBS)  # $(LD_VIA_FILE)
	$(QUIET)echo Creating $@ ...
	$(QUIET)if [ ! -d $(BIN_DIR_NAME) ]; then mkdir $(BIN_DIR_NAME); fi;
	$(QUIET)$(LD) $(OBJS) $(LDFLAGS) $(LIBS) -o $@ -lm
	$(QUIET)echo done.

clean-self : clean-obj-self
	$(QUIET)if [ $(BIN_DIR_NAME) ]; then rm -rf $(BIN_DIR_NAME); fi
	$(QUIET)if [ $(LIB_DIR_NAME) ]; then rm -rf $(LIB_DIR_NAME); fi;

clean-obj-self : 
	$(QUIET)if [ $(OBJ_DIR_NAME) ]; then rm -rf $(OBJ_DIR_NAME); fi;

distclean-self : clean-self

convert : $(CVT_FILE)

$(CVT_FILE) : $(ELF_FILE)
	$(QUIET)echo Converting to $@ from $< ...
	$(QUIET)if [ ! -d $(BIN_DIR_NAME) ]; then mkdir $(BIN_DIR_NAME); fi;
	$(QUIET)$(CV) $(CVFLAGS) $< $@
###	$(QUIET)$(CV) $(CVFLAGS) $(CVT_SDRAM_SEC) $< $(CVT_FILE_SDRAM)
###	$(QUIET)$(CV) $(CVFLAGS) $(CVT_DATA_SEC) $< $(CVT_FILE_DATA)
	$(QUIET)echo done.

lib-self : $(LIB_FILE)

$(LIB_FILE) : $(OBJS)
	$(QUIET)echo Creating $@ ...
	$(QUIET)if [ ! -d $(LIB_DIR_NAME) ]; then mkdir $(LIB_DIR_NAME); fi;
	$(QUIET)$(AR) $(ARFLAGS) $@ $(OBJS)
	$(QUIET)$(AR) -t $@ > $(LIB_INFO_FILE)
	$(QUIET)echo done.
